package fx.game.demo.entity;

import fx.game.demo.common.BaseMeta;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

/**
 * 矩形与数字
 */
public class RectangleAndNumber
        extends BorderPane
        implements Cloneable {

    //对应数字代表的颜色
    private final static String[] COLORS = {
            "#f6f5ec",
            "#84bf96",
            "#50b7c1",
            "#f3715c",
            "#ffce7b",
            "#f173ac",
            "#009ad6",
            "#f47920",
            "#00ae9d",
            "#8552a1",
            "#ffd400",
            "#543044",
            "#96582a",
            "#a1a3a6",
            "#444693",
            "#000000"
    };

    public final static Integer COLORS_LENGTH = COLORS.length;

    private Text text;

    /**
     * 当前方块显示的数字
     */
    private Integer number;

    /**
     * 在父节点中的位置坐标
     */
    private double locationX = 0.0;

    /**
     * 在父节点中的位置坐标
     */
    private double locationY = 0.0;

    /**
     * 初始化
     *
     * @param i         颜色
     * @param n         显示数字
     */
    public RectangleAndNumber(int i, Integer n) {
        this.number = n;
        setMaxSize(BaseMeta.RECTANGLE_WIDTH, BaseMeta.RECTANGLE_HEIGHT);
        setMinSize(BaseMeta.RECTANGLE_WIDTH, BaseMeta.RECTANGLE_HEIGHT);
        setPrefSize(BaseMeta.RECTANGLE_WIDTH, BaseMeta.RECTANGLE_HEIGHT);

        if(n == null || i < 0 || n < 2) {
            text = new Text();
            setStyle("-fx-background-color:" + COLORS[0] +
                    ";-fx-border-radius: 10px;");
        }else {
            text = new Text(n.toString());
            text.setFont(Font.font(20));
            text.setFill(Paint.valueOf("#FFFFFF"));
            setCenter(text);

            //设置背景颜色
            setStyle("-fx-background-color:" + COLORS[i] +
                    ";-fx-background-radius: 10px;");
        }

    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public double getLocationX() {
        return locationX;
    }

    public void setLocationX(double locationX) {
        this.locationX = locationX;
    }

    public double getLocationY() {
        return locationY;
    }

    public void setLocationY(double locationY) {
        this.locationY = locationY;
    }
}
